package com._58city.spark.app;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com._58city.spark.app.ext.DaoUtil;
import com._58city.spark.app.ext.dto.BelongCate;
import com._58city.spark.app.ext.dto.DispCate;
import com._58city.spark.app.ext.dto.DispCity;
import com._58city.spark.app.ext.dto.ImcInfoSource;
import com._58city.spark.app.ext.dto.ResumeSource;
import com._58city.spark.app.redis.Constant;
import com._58city.spark.app.redis.RedisUtil;

/**
 * @author lihao 缓存工具类
 */
public class CacheUtil {

	public static Set<Long> directSalesCity = new HashSet<Long>(27); // 直营城市列表
	static {
		directSalesCity.add(1l); // 北京
		directSalesCity.add(102l); // 成都
		directSalesCity.add(37l); // 重庆
		directSalesCity.add(241l); // 石家庄
		directSalesCity.add(265l); // 济南
		directSalesCity.add(122l); // 青岛
		directSalesCity.add(172l); // 南京
		directSalesCity.add(79l); // 杭州
		directSalesCity.add(5l); // 苏州
		directSalesCity.add(93l); // 无锡
		directSalesCity.add(837l); // 合肥
		directSalesCity.add(158l); // 武汉
		directSalesCity.add(342l); // 郑州
		directSalesCity.add(414l); // 长沙
		directSalesCity.add(304l); // 福州
		directSalesCity.add(606l); // 厦门
		directSalesCity.add(845l); // 南宁
		directSalesCity.add(202l); // 哈尔滨
		directSalesCity.add(319l); // 长春
		directSalesCity.add(740l); // 太原
		directSalesCity.add(147l); // 大连
		directSalesCity.add(188l); // 沈阳
		directSalesCity.add(2l); // 上海
		directSalesCity.add(18l); // 天津
		directSalesCity.add(3l); // 广州
		directSalesCity.add(4l); // 深圳
		directSalesCity.add(413l); // 东莞
	}

	public synchronized static void init() {
		// 展现类别
		List<DispCate> list = DaoUtil.getDispCateDao().select_all();
		Map<String, Object> tmp = new HashMap<String, Object>();
		for (DispCate one : list) {
			if (one.getCate_id() > 0) {
				tmp.put(String.valueOf(one.getCate_id()), one);
			}
		}
		RedisUtil.cache.hMultiSetObject(Constant.redis_cate_dict, tmp);

		// //归属类别
		// List<BelongCate> belongCateList =
		// DaoUtil.getBelongCateDao().select_all();
		// Map<String, Object> belongCateMap = new HashMap<String, Object>();
		// for (BelongCate one : belongCateList) {
		// if (one.getCate_id() > 0) {
		// belongCateMap.put(String.valueOf(one.getCate_id()), one);
		// }
		// }
		// RedisUtil.cache.hMultiSetObject(Constant.redis_belong_cate_dict,
		// belongCateMap);
		//
		//
		// //IMC来源
		// List<ImcInfoSource> imcInfoSourceList =
		// DaoUtil.getImcInfoSourceDao().select_all();
		// Map<String, Object> imcInfoSourceMap = new HashMap<String, Object>();
		// for (ImcInfoSource one : imcInfoSourceList) {
		// imcInfoSourceMap.put(String.valueOf(one.getSource()), one);
		// }
		// RedisUtil.cache.hMultiSetObject(Constant.redis_imc_source_dict,
		// imcInfoSourceMap);
	}

	/* 以下为从数据库获取各种字典表的MAP */

	public static Map<Long, DispCate> cateMap() {
		List<DispCate> list = DaoUtil.getDispCateDao().select_all();
		Map<Long, DispCate> map = new HashMap<Long, DispCate>();
		for (DispCate one : list) {
			if (one.getCate_id() > 0)
				map.put(one.getCate_id(), one);
		}
		return map;
	}

	public static Map<Long, DispCity> cityMap() {
		List<DispCity> list = DaoUtil.getDispCityDao().select_all();
		Map<Long, DispCity> map = new HashMap<Long, DispCity>();
		for (DispCity one : list) {
			map.put(one.getCity_id(), one);
		}
		return map;
	}

	public static Map<Long, BelongCate> belongCateMap() {
		List<BelongCate> list = DaoUtil.getBelongCateDao().select_all();
		Map<Long, BelongCate> map = new HashMap<Long, BelongCate>();
		for (BelongCate one : list) {
			if (one.getCate_id() > 0)
				map.put(one.getCate_id(), one);
		}
		return map;
	}

	public static Map<String, ImcInfoSource> imcInfoSourceMap() {
		List<ImcInfoSource> list = DaoUtil.getImcInfoSourceDao().select_all();
		Map<String, ImcInfoSource> map = new HashMap<String, ImcInfoSource>();
		for (ImcInfoSource one : list) {
			map.put(one.getSource(), one);
		}
		return map;
	}

	public static Map<Integer, ResumeSource> resumeSourceMap() {
		List<ResumeSource> list = DaoUtil.getResumeSourceDao().select_all();
		Map<Integer, ResumeSource> map = new HashMap<Integer, ResumeSource>();
		for (ResumeSource one : list) {
			map.put(one.getCode_id(), one);
		}
		return map;
	}

	/* 以下为从redis里 根据key获取对应的各类字典VO */

	public static DispCate getCateDict(long cateid) {
		String key = String.valueOf(cateid);
		return (DispCate) RedisUtil.cache.hGetObject(Constant.redis_cate_dict, key);
	}

	// public static BelongCate getBelongCateDict(long cateid) {
	// String key = String.valueOf(cateid);
	// return
	// (BelongCate)RedisUtil.cache.hGetObject(Constant.redis_belong_cate_dict,
	// key);
	// }
	//
	//
	// public static ImcInfoSource getImcSourceDict(String source) {
	// return
	// (ImcInfoSource)RedisUtil.cache.hGetObject(Constant.redis_imc_source_dict,
	// source);
	// }

}
